package com.ironman.compute;


import cn.hutool.core.collection.ListUtil;
import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import com.ironman.common.entity.SpeedData;
import com.ironman.common.OriginalSimpleData;
import com.ironman.event.topic.dami.OriginalTopic;
import com.ironman.common.util.AccuracyUtil;
import lombok.extern.slf4j.Slf4j;
import org.noear.dami.Dami;

import java.util.Date;
import java.util.List;
import java.util.function.BiConsumer;

@Slf4j
public class SpeedAverageComputer extends BaseAverageComputer<SpeedData> {

    public SpeedAverageComputer(Integer windowSize) {
        super(windowSize);
    }

    public void init(BiConsumer<Date, Double> speedConsumer) {
        Dami.<OriginalSimpleData, Void>bus().listen(OriginalTopic.ORIGINAL_TOPIC + GPS, payload -> {
            OriginalSimpleData originalSimpleData = payload.getContent();
            Date ts = originalSimpleData.getTs();
            String data = originalSimpleData.getData();
            String[] split = data.split(",");
            computeWindowData(new SpeedData(ts, Double.parseDouble(split[3])), speedConsumer);
        });
    }


    public void computeWindowData(SpeedData speedData, BiConsumer<Date, Double> speedConsumer) {
        windowData(speedData, speedConsumer);
    }

    public static void main(String[] args) {
        SpeedAverageComputer speedComputer = new SpeedAverageComputer(2);

        List<SpeedData> speedDataList = ListUtil.of(
                new SpeedData(DateUtil.parse("2024-12-15 12:00:00", DatePattern.NORM_DATETIME_PATTERN), 172.5467D),
                new SpeedData(DateUtil.parse("2024-12-15 12:00:01", DatePattern.NORM_DATETIME_PATTERN), 131.9738D),
                new SpeedData(DateUtil.parse("2024-12-15 12:00:02", DatePattern.NORM_DATETIME_PATTERN), 139.6231D),
                new SpeedData(DateUtil.parse("2024-12-15 12:00:03", DatePattern.NORM_DATETIME_PATTERN), 139.6231D),
                new SpeedData(DateUtil.parse("2024-12-15 12:00:04", DatePattern.NORM_DATETIME_PATTERN), 149.9448D),
                new SpeedData(DateUtil.parse("2024-12-15 12:00:05", DatePattern.NORM_DATETIME_PATTERN), 180.8973D),
                new SpeedData(DateUtil.parse("2024-12-15 12:00:06", DatePattern.NORM_DATETIME_PATTERN), 106.9091D),
                new SpeedData(DateUtil.parse("2024-12-15 12:00:07", DatePattern.NORM_DATETIME_PATTERN), 129.4654D),
                new SpeedData(DateUtil.parse("2024-12-15 12:00:08", DatePattern.NORM_DATETIME_PATTERN), 200.2282D),
                new SpeedData(DateUtil.parse("2024-12-15 12:00:09", DatePattern.NORM_DATETIME_PATTERN), 119.9718D)
        );
        for (SpeedData speedData : speedDataList) {
            speedComputer.computeWindowData(speedData, new BiConsumer<Date, Double>() {
                @Override
                public void accept(Date ts, Double average) {
                    log.info("{}=>当前平均船速{}cm/s", ts, AccuracyUtil.halfUpSpeedDouble(average));
                }
            });
        }

    }

}
